#define _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"


void STInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

void STDesTory(ST* ps)
{
	assert(ps);
	if (ps->arr != NULL)
	{
		free(ps->arr);
		ps->arr = NULL;
	}
	ps->top = ps->capacity = 0;
	
}

void STPush(ST* ps, Datatype s)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		Datatype* tmp = (Datatype*)realloc(ps->arr, newCapacity*sizeof(Datatype));
		if (tmp == NULL)
		{
			perror("realloc");
			return 1;
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
	}
	ps->arr[ps->top++] = s;

}

void STErase(ST* ps)
{
	assert(ps && ps->top);
	ps->top--;
}

Datatype STTop(ST* ps)
{
	assert(ps && ps->top);
	return ps->arr[ps->top - 1];
}

Datatype STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}

